/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

interface User {
  id: number;
  name: string;
  role: string;
}
export type UserPick2 = Pick<User, 'name'>;
export type UserPick3 = Pick<User, 'role' | 'id'>;
export type UserPick4 = Pick<User, keyof User>;

interface User1 {
  readonly id: number;
  name?: string;
}
export type UserPick5 = Pick<User1, 'id'>;
export type UserPick6 = Pick<User1, 'name'>;
//Omit
export type UserOmit1 = Omit<User, 'id'>;
export type UserOmit2 = Omit<User, 'id' | 'name'>;
export type UserOmit3 = Omit<User, keyof User>;
export type UserOmit4 = Omit<User1, 'id'>;
export type UserOmit5 = Omit<User1, 'name'>;
//Exclude
type UnionType = string | number | boolean;
export type ExcludeNumber = Exclude<UnionType, number>;
export type ExcludedType = Exclude<UnionType, boolean | string>;
type KeyUnion = 'id' | 'name' | 'email' | 'password';
export type ExcludedKeyUnion = Exclude<KeyUnion, 'password'>;
export type ExcludedKeyUnion1 = Exclude<KeyUnion, 'password' | 'email' | 'id'>;
//Extract
type StringOrNumberOrBoolean = string | number | boolean;
export type ExtractedString = Extract<StringOrNumberOrBoolean, string>;
export type ExtractedString1 = Extract<StringOrNumberOrBoolean, string | number>;
type KeyUnion1 = 'id' | 'name' | 'email' | 'password';
export type ExcludedKeyUnion2 = Extract<KeyUnion1, 'password'>;
export type ExcludedKeyUnion3 = Extract<KeyUnion1, 'password' | 'name'>;
//NonNullable
type NullableType = string | number | null;
type NullableType1 = string | number | undefined;
type NullableType2 = string | number | null | undefined;
export type NonNullType = NonNullable<NullableType>;
export type NonNullType1 = NonNullable<NullableType1>;
export type NonNullType2 = NonNullable<NullableType2>;
//Parameters<Type>
let func001 = (name: string) => {
};
export type FuncParams = Parameters<typeof func001>;
export type Params2 = Parameters<typeof func001>[0];
let func002 = () => {
};
export type Params3 = Parameters<typeof func002>;
//ConstructorParameters<Type>
class Person {
  constructor(public name: string, public age: number) {}
}
export type PersonConstructorParams = ConstructorParameters<typeof Person>;
export function createPerson(
  ...args: ConstructorParameters<typeof Person>
): Person {
  return new Person(...args);
}
//ReturnType<Type>
function add(a: number, b: number): number {
  return a + b;
}
function str(a: string): string {
  return a;
}
export type ReturnTypeOfAdd = ReturnType<typeof add>;
export type ReturnTypeOfMore = ReturnType<typeof add> | ReturnType<typeof str>;
interface State {
  set<T>(key: string, value: T): boolean;
}
export type StateSetterReturn = ReturnType<State['set']>;
//InstanceType<Type>
export class UserIns {
  name: string;
  age: number;

  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }
}
export type UserInstance = InstanceType<typeof UserIns>;

export function createInstance<T extends new (...args: any[]) => any>(Class: T, ...args: ConstructorParameters<T>): InstanceType<T> {
  return new Class(...args);
}